#pragma once

#include "BaseReader.h"

class CBufReader : public CBaseReader
{
public:
  CBufReader() : m_pData(NULL), m_lSize(0), m_lPos(0)
  {}
  CBufReader(const BYTE* pData, long lSize) : m_pData(NULL), m_lSize(0), m_lPos(0)
  { Open(pData, lSize); }
  ~CBufReader()
  { Close(); }

  bool Open(const BYTE* pData, long lSize)
  {
    m_pData = pData;
    m_lSize = lSize;
    m_lPos = 0;
    m_nLine = 0;
    DetectUtf8();
    return true;
  }
  void Close()
  {
    m_pData = NULL;
    m_lSize = 0;
    m_lPos = 0;
    m_nLine = 0;
    m_bUtf8 = false;
  }

  long GetPosition() const
  { return m_lPos; }
  bool SetPosition(long pos)
  { if (pos >= 0 && pos < m_lSize) { m_lPos = pos; return true; } else return false; }

  bool ReadLine(CString& s);

  int  GetLine() const
  { return m_nLine; }

protected:
  void DetectUtf8();

protected:
  const BYTE* m_pData;
  long  m_lSize;
  long  m_lPos;
};
